package com.attendance.www.hr.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.RestVacationBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IRestVacationService;
import com.zhangkongli.frame.core.ResultMap;

/**
 * 假期额度
 * 
 * @version 2017/9/25-2017/9/27
 * @author 徐伟，张鑫
 *
 */
@Controller
@RequestMapping("/restVacation")
public class RestVacationController {

	@Resource
	private IRestVacationService restVacationService;

	/**
	 * 查询假期额度信息
	 * 
	 * @param request
	 * @param restVacationBean
	 * @return
	 */
	@RequestMapping(value = "/getRestVacationList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRestVacationList(HttpServletRequest request, @RequestBody RestVacationBean restVacationBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		Integer PrivilegeGroupId = userEmployee.getPrivilegeGroupId();
		if (PrivilegeGroupId != 1) {
			restVacationBean.setEmployeeId(userEmployee.getEmployeeId());
		}
		return ResultMap.success(restVacationService.getRestVacationList(restVacationBean));
	}

	/**
	 * 根据类型查询假期额度信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRestVacationByType.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getRestVacationByType(HttpServletRequest request  ,@RequestBody  RestVacationBean restVacationBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		if (restVacationBean.getFurloughType() == null) {
				throw new ResultFaultException("0_00001");
			}
		if(restVacationBean.getFurloughType()==1&&restVacationBean.getStartDate()==null) {
			restVacationBean.setTotalVacation(new BigDecimal(0));
			restVacationBean.setUsedVacation(new BigDecimal(0));
			restVacationBean.setRestVacation(new BigDecimal(0));
			return  ResultMap.success(restVacationBean);
		}
		
		Integer employeeId = userEmployee.getEmployeeId();
		return ResultMap.success(restVacationService.getRestVacationByType(restVacationBean, employeeId));
	}
	
	/**
	 * 在1,2月份查询去年和今年的年假余额
	 * @param request
	 * @param restVacationBean
	 * @return
	 */
	@RequestMapping(value = "/getLastYearYearFurlough.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getLastYearYearFurlough(HttpServletRequest request  ,@RequestBody  RestVacationBean restVacationBean) {
		UserEmployeeBean userEmployee = (UserEmployeeBean) request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if (userEmployee == null) {
			throw new ResultFaultException("1_00001");
		}
		/*if (restVacationBean.getFurloughType() == null) {
				throw new ResultFaultException("0_00001");
		}*/
		if(restVacationBean.getStartDate()==null) {
			throw new ResultFaultException("0_00001");
		}
		Integer employeeId = userEmployee.getEmployeeId();
		return ResultMap.success(restVacationService.getLastYearYearFurlough(restVacationBean, employeeId));
	}
	
}
